package com.spark.colliery.module.home

import android.os.Bundle
import android.view.View
import com.blankj.utilcode.util.LogUtils
import com.spark.colliery.MainFragment
import com.spark.colliery.R
import com.spark.common.BaseFragment
import com.spark.colliery.module.home.fragment.HomeAdminManagerFragment
import com.spark.colliery.module.home.fragment.HomeCommonFragment
import com.tbruyelle.rxpermissions2.RxPermissions
import me.yokeyword.fragmentation.SupportFragment

class HomeFragment : BaseFragment(), HomeCommonFragment.OnSwitchListener, HomeAdminManagerFragment.OnBackClickListener {
    companion object {
        fun newInstance(): HomeFragment {
            val args = Bundle()
            val fragment = HomeFragment()
            fragment.arguments = args
            return fragment
        }
    }

    private lateinit var homeCommon: HomeCommonFragment
    private var homeAdmin: HomeAdminManagerFragment? = null

    override fun contentResId(): Int {
        return R.layout.frag_home
    }


    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        switchToHome()
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        activity?.apply {
            val rp = RxPermissions(this)
            rp.setLogging(true)
            rp.requestEach(android.Manifest.permission.CAMERA)
                    .subscribe {
                        when {
                            it.shouldShowRequestPermissionRationale -> LogUtils.e("【Perm】raiseRationale")
                            else -> LogUtils.e(it.name)
                        }
                    }
        }
    }

    fun switchToHome() {
        homeCommon = com.spark.colliery.module.home.fragment.HomeCommonFragment.newInstance()
        homeCommon.setOnSwtichListener(this)
        childFragmentManager
                .beginTransaction()
                .add(R.id.fl_content, homeCommon)
                .commit()
    }

    /**
     * 切换管理页
     */
    override fun switchToAdmin() {
        homeAdmin = com.spark.colliery.module.home.fragment.HomeAdminManagerFragment.newInstance()
        homeAdmin!!.setOnBackClickListener(this)
        childFragmentManager
                .beginTransaction()
                .replace(R.id.fl_content, homeAdmin)
                .addToBackStack(com.spark.colliery.module.home.fragment.HomeAdminManagerFragment::class.java.simpleName)
                .commit()
    }

    fun checkBack(): Boolean {
        if (homeAdmin == null) {
            return true
        }
        return !homeAdmin!!.isVisible
    }


    fun refresh() {
        if (homeCommon != null)
            homeCommon.fetchHomeOptions()
    }


    /**
     * 弹出
     */
    public override fun pop() {
        val backCount = childFragmentManager.backStackEntryCount
        if (backCount >= 1) {
            childFragmentManager.popBackStack()
        }
    }

    /**
     * 管理页点击首页后退到首页
     */
    override fun onBackClicked() {
        pop()
    }

    fun startBrother(target: SupportFragment) {
        parentFragment.let { it as MainFragment }.start(target)
    }

}
